let arr = [
    {key: 'a', value: 1}, 
    {key: 'a', value: 2}, 
    {key: 'a', value: 2}, 
    {key: 'a', value: '3'}, 
    {key: 'b', value: 4}
] 

let last = {}


let primary = arr[0].key
last[primary] = []

for(let i=0;i<arr.length;i++) {
    if(arr[i].key === primary) {
        if(typeof arr[i].value == 'number') {
            last[primary].push(arr[i].value)
        }
        else{
            last[primary].push(parseInt(arr[i].value))
        }
    }else{
        last[arr[i].key] = []
        primary = arr[i].key

        if(arr[i].key === primary) {
            if(typeof arr[i].value == 'number') {
                last[primary].push(arr[i].value)
            }
            else{
                last[primary].push(parseInt(arr[i].value))
            }
        }
    }
}

for(key in last){
    if(last[key].length <= 1) {
        last[key] = last[key][0]
    }else{
        last[key] = Array.from(new Set(last[key]))
    }
}

console.log(last)